//
//  UIControl+Block.m
//
//  Created by zhaol on 10/26/15.
//  Copyright © 2015 zify. All rights reserved.
//

#import "UIControl+Block.h"

#import <objc/runtime.h>

static char key_UIControlEventBlock;

@implementation UIControl(Block)

- (void)eventHandler
{
    if (self.handleEventBlock) {
        self.handleEventBlock(self);
    }
}

#pragma mark - block
- (void)setHandleEventBlock:(void (^)(UIControl *sender))eventBlock
{
    objc_setAssociatedObject(self, &key_UIControlEventBlock, eventBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    
    if (eventBlock) {
        [self addTarget:self action:@selector(eventHandler) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void (^) (UIControl *)) handleEventBlock
{
    return objc_getAssociatedObject(self, &key_UIControlEventBlock);
}

@end
